#### Replication Code for:
#### Legislative Cooptation in Authoritarian Regimes: Policy Cooperation in the Kuwait National Assembly
#### Authors: Daniel Tavana and Erin York
#### Created: 07/22/24


rm(list = ls())
library(tidyverse)
library(stargazer)
library(xtable)
library(logr)

options("logr.autolog" = TRUE)

# Open the log
lf <- log_open(file.path("logmain.log"))

# Send message to log
log_code()


# load data ---------------------------------------------------------------

load("df_cooptationdat.rdata")

# if using csv formatted data, uncomment the following lines to read in csvs
#votedf<- read_csv("df_kna_rcv.csv")
#lawdat<- read_csv("df_kna_laws.csv")
#oildat<- read_csv("df_kw_oil_monthly.csv")
#kw_gdp<- read_csv("df_kw_gdp.csv")

# Construct DV ------------------------------------------------------------


# generate summary data on minister behavior at the lawvote level

mins<- votedf %>%
  filter(min_exact == 1, !is.na(vote)) %>%
  group_by(lawcodes, term, term_year) %>% 
  summarise(minpresent = n(),
            minyes = sum(as.numeric(vote == "yes")),
            minno = sum(as.numeric(vote == "no")),
            minabs = sum(as.numeric(vote == "abstain")),
            minsupport_abs = minyes/minpresent,
            minsupport_yn = minyes/(minyes+minno),
            minagainst_yn = minno/(minyes + minno),
            minabs_prop = minabs/minpresent,
            minfavor = as.numeric(minyes > minno))


# votes where all ministers abstained
allabs<- filter(mins, minyes == 0 & minno == 0)

# votes where ministers split (yes/no)
splitvotes<- filter(mins, minyes > 0 & minno >0)

# add info on minister stance to vote data
mpdf<- mins %>% 
  select(lawcodes, term, minfavor) %>% 
  full_join(votedf) %>%
  filter(!is.na(vote), mp_exact == 1) %>%
  # drop votes where no ministers took a stand and split votes
  filter(!(lawcodes %in% allabs$lawcodes)) %>%
  filter(!(lawcodes %in% splitvotes$lawcodes)) %>%
  mutate(rulechange = as.numeric(date > ymd("2007-04-29"))) %>% 
  # code agreement with ministers - alternate conceptualizations
  # vote yes on favor, no on unfavor, 0 otherwise
  mutate(votewithmin = ifelse(minfavor == 1 & vote == "yes", 1, 
                              ifelse(minfavor == 0 & vote == "no", 1, 0)),
         # count abstain as favor
         votewithmin_abs = ifelse(minfavor == 1 & vote == "yes"|vote == "abstain", 1, 
                                  ifelse(minfavor == 0 & vote == "no"|vote == "abstain", 1, 0)),
         # count abstain as against
         abs_no = ifelse(minfavor == 1 & vote == "yes", 1,
                         ifelse((minfavor == 0 & vote == "no"), 1,
                                ifelse(minfavor == 0 & vote == "abstain", 1, 0)))) %>%
  # primary DV - incorporates rule change in which abstentions after 2007 were effective noes
  mutate(votewithmin_rulechange = ifelse(rulechange == 0, votewithmin_abs,
                                         ifelse(vote == "abstain", 0, votewithmin))) 




# Table 1 -----------------------------------------------------------------



leg_only<- votedf %>% filter(mp_exact == 1)

# Table 1 stats
# top panel - legislator votes
# NA = absent during vote
leg_n<- table(leg_only$vote, useNA = "ifany")
leg_prop<- prop.table(table(leg_only$vote, useNA = "ifany"))


min_only<- votedf %>% filter(min_exact == 1) 

# bottom panel - minister votes
# NA - absent during vote
min_n<- table(min_only$vote, useNA = "ifany")
min_prop<- prop.table(table(min_only$vote, useNA = "ifany"))

actions<- c("Abstain", "No", "Yes", "Absent", "Total")
t1<- data_frame(Category = c(rep("Legislator votes", 5), rep("Minister votes", 5)),
                Action = rep(actions, 2),
                N = c(leg_n, nrow(leg_only),
                      min_n, nrow(min_only)),
                Prop = c(leg_prop, 1,
                         min_prop, 1)) %>%
  mutate(Action = factor(Action, levels = c("Yes", "No", "Abstain", "Absent", "Total"))) %>%
  arrange(Category, Action)

sink(file = "Table1.tex")
print(xtable(t1), include.rownames = F)
sink()





# Table 2 -----------------------------------------------------------------



# regression dataset
dat<- mpdf %>%
  # drop abstentions pre-2007
  filter(!(date <= ymd("2007-04-29") & vote == "abstain"),
         # drop missing votes
         !is.na(vote),
         # drop individuals who were ministers at the time
         min_exact == 0) 



reg1a<-  (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
               ideology_binary + ideoaffil_n_lag +  
               age_imp + female + edu_imp + 
               career +shia +
               tribe_binary + log(swissvote_imp)+byelection +
               inflation_imp +
               type_budget + type_treaty + type_general + law_new, dat))


reg1b<- (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
              ideology_binary + ideoaffil_n_lag +  
              unique_id +
              inflation_imp +
              type_budget + type_treaty + type_general + law_new, dat))


reg2a<- (lm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
              ideology_binary + ideoaffil_n_lag +  
              age_imp + female + edu_imp + 
              career +shia +
              tribe_binary +log(swissvote_imp)+byelection +
              inflation_imp +
              type_budget + type_treaty + type_general + law_new, dat))

reg2b<- (lm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
              ideology_binary + ideoaffil_n_lag +  
              unique_id +
              inflation_imp +
              type_budget + type_treaty + type_general + law_new, dat))

# abbreviated regression for main text 
# Table 2
sink("Table2.tex")
stargazer(reg1a, reg1b, reg2a, reg2b,
          keep = c("wticonstant*", "oil_gross*", "infl","ideology_binary", "ideoaffil*"),
          covariate.labels = c("WTI Price", "Oil Revenues", 
                               "Ideology Dummy", "Cabinet Affiliate",
                               "Inflation"),
          add.lines = list(c("Term FEs", rep("\\checkmark", 4)),
                           c("MP FEs", rep(c("", "\\checkmark"), 2)),
                           c("MP controls",rep(c("\\checkmark", ""), 2)),
                           c("Law controls", rep("\\checkmark", 4))),
          dep.var.labels = "Voting with regime",
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.05, 0.01, 0.001))
sink()




# Table 3 -----------------------------------------------------------------


# mechanism exploration:

## affiliated minister present for vote
present1<- (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                 ideology_binary + ideo_n_present+
                 age_imp + female + edu_imp + 
                 career +shia +
                 tribe_binary + log(swissvote_imp) + byelection +
                 inflation_imp +
                 type_budget + type_treaty + type_general + law_new, dat)) 

present2<- (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                 ideology_binary +ideoaffil_n_lag+ ideo_n_present+
                 age_imp + female + edu_imp + 
                 career +shia +
                 tribe_binary + log(swissvote_imp) + byelection +
                 inflation_imp +
                 type_budget + type_treaty + type_general + law_new, dat))      



## affiliated minister holds relevant portfolio; restricted to relevant topics
topic1<- (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
               ideology_binary + ideo_topic_binary+
               age_imp + female + edu_imp + 
               career +shia +
               tribe_binary + log(swissvote_imp)+byelection +
               inflation_imp +
               type_budget + type_treaty + type_general + law_new, 
             dat %>% filter(topic_religion == 1|
                              topic_housing == 1|
                              topic_govt == 1|
                              topic_edu == 1)))  

topic2<- (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
               ideology_binary +ideoaffil_n_lag+ ideo_topic_binary+
               age_imp + female + edu_imp + 
               career +shia +
               tribe_binary + log(swissvote_imp)+byelection +
               inflation_imp +
               type_budget + type_treaty + type_general + law_new, 
             dat %>% filter(topic_religion == 1|
                              topic_housing == 1|
                              topic_govt == 1|
                              topic_edu == 1)))  



## Table 3
sink("Table3.tex")
stargazer(present1, present2, topic1, topic2,
          keep = c("wticonstant*", "oil_gross*", "ideo*", "infl"),
          covariate.labels = c("WTI Price",
                               "Ideology Dummy", 
                               "Cabinet Affiliate",
                               "Affiliates Present", 
                               "Topical Portfolio",
                               "Inflation"),
          add.lines = list(
            c("Term FEs",  rep("\\checkmark", 4)),
            c("MP controls", rep(c("\\checkmark"), 4)),
            c("Law controls", rep("\\checkmark", 4)),
            c("Topical Subset", "", "", "\\checkmark", "\\checkmark")),
          dep.var.labels = c("Voting with regime"),
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.05, 0.01, 0.001), 
          star.char = c("*", "**", "***"))
sink()



# Table 4 -----------------------------------------------------------------

# interaction of oil price and ideology indicator

reg3a<- (lm(votewithmin_rulechange ~ wticonstant_lag_std *ideology_binary + term  + 
              age_imp + female + edu_imp + 
              career +shia +
              tribe_binary + log(swissvote_imp) + byelection +
              inflation_imp +
              type_budget + type_treaty + type_general + law_new, dat))

reg3b<- (lm(votewithmin_rulechange ~ oil_gross_lag_std *ideology_binary +  term + 
              age_imp + female + edu_imp + 
              career +shia +
              tribe_binary + log(swissvote_imp) + byelection +
              inflation_imp +
              type_budget + type_treaty + type_general + law_new, dat))


# Table 4
sink("Table4.tex")
stargazer(reg3a, reg3b,
          keep = c("wticonstant*", "oil_gross*", "ideology_binary", "infl"),
          covariate.labels = c("WTI Price", "Oil Revenues", 
                               "Ideology Dummy", "Inflation", "WTI x Ideo", "Oil x Ideo"),
          add.lines = list(
            c("Term FEs",  rep("\\checkmark", 4)),
            c("MP controls", rep("\\checkmark", 2)),
            c("Law controls", rep("\\checkmark", 2))),
          dep.var.labels = c("Voting with regime"),
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          star.char = c("+", "*", "**", "***"))
sink()
# note - there is an error in the table notes which must be manually corrected
# \multicolumn{2}{r}{$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01; $^{***}$p$<$0.001} 


# Figure 1 ----------------------------------------------------------------

## See 03_rep_external.R for replication code

# Figure 2 ----------------------------------------------------------------


mindate<- ymd("1963-01-01")
maxdate<- ymd("2016-01-01")

f2_left<- oildat %>% mutate(key = "WTI price") %>%
  ggplot(aes(x = monthyear, y = wticonstant, lty = key)) + geom_line() +
  xlim(c(mindate, maxdate)) +
  theme_bw() +
  xlab("") +
  ylab("Constant 2016 Dollars") +
  theme(legend.position = "bottom", legend.title = element_blank())


f2_right<- kw_gdp %>% 
  # GDP in constant 2016 dollars
  mutate(gdp_adj = gdp/inflation_multiplier) %>%
  gather(key, value, oilrents_adj:gdp_adj) %>% 
  mutate(key2 = ifelse(key == "oilrents_adj", "Oil Rents", "GDP")) %>%
  ggplot(aes(x = year, y = value, lty = key2)) + geom_line() +
  xlim(c(year(mindate), year(maxdate))) +
  theme_bw() +
  xlab("") +
  ylab("Constant 2016 Dollars") +
  theme(legend.position = "bottom", legend.title = element_blank())

ggsave(f2_left, file = "Figure2_leftpanel.pdf",
      width = 5, height = 5)

ggsave(f2_right, file = "Figure2_rightpanel.pdf",
       width = 5, height = 5)



# Figure 3 ----------------------------------------------------------------



terms<- c("1963", "1967", "1971", "1975", "1981", 
          "1985", "1992", "1996", "1999", "2003",
          "2006", "2008", "2009", "2012.1", "2012.2",
          "2013", "2016")
legstarts<- c("1963-1-23", "1967-1-25", "1971-1-23", "1975-1-27", "1981-2-23",
              "1985-2-26", "1992-10-5", "1996-10-7", "1999-7-3", "2003-7-6",
              "2006-6-29", "2008-5-17", "2009-5-16", "2012-2-1", "2012-12-1",
              "2013-7-27", "2016-11-29")
parls<- data_frame(legstarts = legstarts[1:16],
                   term_year = terms[1:16])



### cabinet characteristics
# Figure 3
f3<- votedf %>%
  mutate(cab_group = ifelse(ideology!= "Unaffiliated", ideology, 
                            ifelse(sabah== 1, "Sabah", "Other"))) %>%
  filter(min_exact == 1) %>% 
  select(date, unique_id, cab_group) %>%
  unique() %>% group_by(date) %>%
  summarise(n(),
            sabah = sum(cab_group == "Sabah"),
            mb = sum(cab_group == "Muslim Brotherhood"),
            islamist = sum(cab_group == "Independent Islamist"),
            lp = sum(cab_group == "Populist-Leftist"),
            natlib = sum(cab_group == "National-Liberal"),
            salafi = sum(cab_group == "Salafi"),
            shia= sum(cab_group == "Shia Islamist"),
            other = sum(cab_group == "Other")) %>% 
  gather(type, number, sabah:other) %>%
  mutate(prop = number/`n()`,
         type = factor(type, levels = c("shia",
                                        "salafi", "islamist", "lp",
                                        "natlib", "mb", "other", "sabah"),
                       labels = c("Shia Islamist",
                                  "Salafi", "Ind. Islamist", "Left-Populist",
                                  "National Liberal", "Muslim Brotherhood","Other",  "Sabah"))) %>%
  ggplot(aes(x = date, y = prop, fill = type)) + geom_area(color = "black") +
  theme_bw() +
  geom_vline(data = parls, aes(xintercept = ymd(legstarts)), lty = 2, color = "DarkGray") + 
  scale_fill_viridis_d(name = "",option = "G", direction = -1, begin = .25) +
  ylab("Proportion of Cabinet") +
  xlab("") +
  theme(legend.position = "bottom") +
  geom_rect(aes(xmin = ymd("1976-8-29"), xmax = ymd("1981-2-23"), ymin = 0.0, ymax = 1.0), fill = "black", alpha = .5) +
  geom_rect(aes(xmin = ymd("1986-7-03"), xmax = ymd("1992-10-5"), ymin = 0.0, ymax = 1.0), fill = "black", alpha = .5)


ggsave(f3, filename = "Figure3.pdf",
       width = 8, height = 5.25)  


# Figure 4 ----------------------------------------------------------------




a<- dat %>% 
  group_by(term_year) %>%
  mutate(termstart = ymd(plyr::mapvalues(term_year, from = terms, to = legstarts))) %>%
  mutate(my = floor_date(date, "quarter")) %>% 
  group_by(term_year, termstart, my) %>%
  summarise(support = mean(votewithmin),
            opposition = 1-support,
            date=first(date)) %>% 
  ungroup() %>%
  mutate(ttl = my - termstart)
a$group <- NA
a$group[a$term_year <= 1977] <- 1
a$group[a$term_year > 1977 & a$term_year < 1987] <- 2
a$group[a$term_year > 1988] <- 3

## figure 4
f4<- ggplot(a, aes(x = date, y = support, group=group)) + geom_line() +
  scale_color_manual(values="black")+
  scale_fill_manual(values=c("black"))+
  theme_bw() +
  ylab("Proportion of MPs voting with minister bloc") + 
  xlab("Year") +
  theme_bw() + 
  geom_vline(data = parls, aes(xintercept = ymd(legstarts)), lty = 2, color = "Gray") + 
  scale_x_date(
    date_labels = "%Y",
    breaks = function(x) seq.Date(from = min(as.Date("1965-01-01")), to = max(as.Date("2015-01-01")), by = "5 years"),
    minor_breaks = NULL,
    expand=c(0.01,0)
  ) +
  scale_y_continuous(
    breaks=seq(0, .9, .1),
    minor_breaks = NULL,
    limits = c(0,1),
    expand = expand_scale(mult = c(0, .02))
  ) + 
  xlab("") +
  theme(panel.grid.major.x=element_blank(), legend.position = "none") 

ggsave(f4, filename = "Figure4.pdf",
      height = 5, width = 8)




# Figure 5 ----------------------------------------------------------------


a<- votedf %>% 
  filter(min_exact == 1, !(term %in% c("kna_rcv_2012_2", "kna_rcv_2012_1"))) %>% 
  select(unique_id, ideology, term_year, monthyear) %>% 
  unique() %>% 
  group_by(term_year) %>% 
  summarise(ideo = mean(ideology != "Unaffiliated")) %>%
  mutate(group = "Cabinet")

b<- votedf %>% 
  filter(mp_exact == 1, !(term %in% c("kna_rcv_2012_2", "kna_rcv_2012_1"))) %>% 
  select(unique_id, ideology, term_year) %>% 
  unique() %>% 
  group_by(term_year) %>% 
  summarise(ideo = mean(ideology != "Unaffiliated")) %>%
  mutate(group = "KNA")


plot_cols<- RColorBrewer::brewer.pal(6, "Paired")

# Figure 5
f5<- a %>% bind_rows(b) %>% 
  ggplot(aes(x = term_year, y = ideo, color = group, shape = group, lty = group)) + 
  geom_smooth() + 
  geom_point(size = 2.5) + 
  theme_bw() + 
  xlab("") +
  ylab("Prop. with Ideological Affiliation") +
  scale_color_manual(name = "",values = c(plot_cols[5], plot_cols[2])) +
  scale_linetype(name = "") +
  scale_shape(name = "") +
  theme(legend.position = "bottom")

ggsave(f5, filename = "Figure5.pdf",
      width= 8, height = 5)



# Table A1 ----------------------------------------------------------------

ta1<-full_join(
  votedf %>%
    filter(!is.na(term_year)) %>%
    mutate(term_year = as.character(term_year)) %>%
    mutate(term_year = case_when(
      term_year == "2012.1" ~ "2012-1",
      term_year == "2012.2" ~ "2012-2",
      TRUE ~ term_year)) %>%
    group_by(term_year) %>%
    summarise(votes = n()),
  lawdat %>%
    filter(!is.na(term_year)) %>%
    group_by(term_year) %>%
    summarise(
      laws = n(),
      missing = sum(is.na(ResponseId)))) %>%
  mutate(missing_prop = missing/laws,
         transcripts = c(228,203,237,107,219,92,243,181,218,158,69,35,118,29,35,122))

# note - this table was manually formatted

sink(file = "TableA1.tex")
print(xtable(ta1), include.rownames = F)
sink()

# Table A2 ----------------------------------------------------------------


## Table A2
# mp dat by term
mpdat<- votedf %>%
  filter(mp_exact == 1) %>%
  select(unique_id, knaed_number, term_year, 
         birthyear, female,
         occ_recent, education,
         shia, ideology,
         tribe_binary, sabah) %>%
  unique() 

# summary stats
mpmeans<- mpdat %>%
  mutate(age = term_year - birthyear,
         edu_known = as.numeric(!is.na(education))) %>%
  mutate(occ_recent = ifelse(is.na(occ_recent), "Missing", occ_recent),
         private = as.numeric(str_detect(occ_recent, "Private")),
         public = as.numeric(str_detect(occ_recent, "Public")),
         other = ifelse(private + public == 0, 1, 0)) %>%
  summarise(Age = mean(age, na.rm = T),
            Female = mean(female), 
            Education_known = mean(edu_known),
            Edu_secondary = mean(education == "secondary", na.rm = T),
            Edu_bachelor = mean(education == "bachelor", na.rm = T),
            Edu_postgraduate = mean(education == "doctorate"| education == "masters", na.rm = T),
            Occ_public = mean(public, na.rm = T),
            Occ_private = mean(private, na.rm = T),
            Occ_other = mean(other, na.rm = T),
            Ideo_none = mean(ideology == "Unaffiliated"),
            Ideo_natlib = mean(ideology == "National-Liberal"),
            Ideo_leftpop = mean(ideology == "Populist-Leftist"),
            Ideo_mb = mean(ideology == "Muslim Brotherhood"),
            Ideo_salafi = mean(ideology =="Salafi"),
            Ideo_islamist = mean(ideology == "Independent Islamist"),
            Ideo_shia = mean(ideology == "Shia Islamist"),
            Tribal = mean(tribe_binary),
            Shia = mean(shia),
            Sabah = mean(sabah, na.rm = T)) %>%
  t()


# min dat by term
mindat<- votedf %>%
  filter(min_exact == 1) %>%
  select(unique_id, knaed_number, term_year, 
         birthyear, female,
         occ_recent, education,
         shia, ideology,
         tribe_binary, sabah) %>%
  unique() 

# summary stats
minmeans<- mindat %>%
  mutate(age = term_year - birthyear,
         edu_known = as.numeric(!is.na(education))) %>%
  mutate(occ_recent = ifelse(is.na(occ_recent), "Missing", occ_recent),
         private = as.numeric(str_detect(occ_recent, "Private")),
         public = as.numeric(str_detect(occ_recent, "Public")),
         other = ifelse(private + public == 0, 1, 0)) %>%
  summarise(age = mean(age, na.rm = T),
            female = mean(female), 
            edu_known = mean(edu_known),
            secondary = mean(education == "secondary", na.rm = T),
            bachelor = mean(education == "bachelor", na.rm = T),
            postgraduate = mean(education == "doctorate"| education == "masters", na.rm = T),
            occ_public = mean(public, na.rm = T),
            occ_private = mean(private, na.rm = T),
            occ_other = mean(other, na.rm = T),
            ideo_none = mean(ideology == "Unaffiliated"),
            ideo_natlib = mean(ideology == "National-Liberal"),
            ideo_leftpop = mean(ideology == "Populist-Leftist"),
            ideo_mb = mean(ideology == "Muslim Brotherhood"),
            ideo_salafi = mean(ideology =="Salafi"),
            ideo_islamist = mean(ideology == "Independent Islamist"),
            ideo_shia = mean(ideology == "Shia Islamist"),
            tribal = mean(tribe_binary),
            shia = mean(shia),
            sabah = mean(sabah)) %>%
  t()

# note - this table was manually formatted

ta2<- data_frame(attribute = c(rownames(mpmeans), "Observations"),
                 Legislators = c(mpmeans, nrow(mpdat)),
                 Ministers = c(minmeans, nrow(mindat)))

sink(file = "TableA2.tex")
print(xtable(ta2), include.rownames = F)
sink()



# Table A3 ----------------------------------------------------------------


laws<- votedf %>%
  select(lawcodes, date, nyes, nno) %>% unique()

lawdat_select<- lawdat %>%
  rename(lawcodes = id) %>%
  select(lawcodes, lawtype, lawtopic, law_title)

## appendix table A3
# 26 votes passed against min opposition
ta3<- filter(mins, minno > 0 & minyes == 0) %>%
  left_join(laws) %>%
  mutate(lawcode = as.numeric(lawcodes)) %>%
  left_join(lawdat_select) %>% 
  arrange(date) %>%
  ungroup() %>%
  mutate(mp_no = nno - minno,
         mp_margin = nyes - mp_no) %>% 
  filter(mp_margin > 0) %>%
  mutate(prediss = c(rep("No", 18), "Yes", rep("No", 4), "Yes","Yes", "No")) %>%
  select(term_year, date, prediss, minno, mp_margin, lawtype, law_title) %>%
  mutate(lawtype = plyr::mapvalues(lawtype, from = c("Budgets", "General_laws"), to = c("Budget", "General")),
         date = as.character(date))
colnames(ta3)<- c("Term", "Date", "Pre-Dissolution", "Min Votes Against", "MP Vote Margin", "Law Type", "Law Subject")

## note - law subject was translated manually by authors
## table was manually formatted for line breaks
sink(file = "TableA3.tex")
print(xtable(ta3, digits = 0), include.rownames = F)
sink()


# Table A4 ----------------------------------------------------------------



# full regression table for appendix
# Table A4
sink("TableA4.tex")
stargazer(reg1a, reg1b, reg2a, reg2b,
          omit = c("term*", "unique*"),
          covariate.labels = c("WTI Price", "Oil Revenues",
                               "Ideology Dummy","Cabinet Affiliate",
                               "Age", "Female", 
                               "Edu - Post-graduate",
                               "Edu - Missing",
                               "Edu - Secondary",
                               "Occ - Other",
                               "Occ - Private",
                               "Occ - Public", 
                               "Shia", "Tribe Dummy",
                               "Log Swiss Vote", 
                               "By-election",
                               "Inflation",
                               "Law Type - Budget", "Law Type - Treaty",
                               "Law Type - General",
                               "New Law"),
          add.lines = list(c("Term FEs", rep("\\checkmark", 6)),
                           c("MP FEs", rep(c("", "\\checkmark"), 3))),
          dep.var.labels = "Voting with regime",
          dep.var.caption = "",
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.05, 0.01, 0.001))
sink()


# Table A5 ----------------------------------------------------------------


# Table A5 
## extended version of Table 4
sink("TableA5.tex")
stargazer(reg3a, reg3b,
          omit = c("term*"),
          covariate.labels = c("WTI Price", "Oil Revenues",
                               "Ideology Dummy",
                               "Age", "Female", 
                               "Edu - Post-graduate",
                               "Edu - Missing",
                               "Edu - Secondary", 
                               "Occ - Other",
                               "Occ - Private",
                               "Occ - Public", 
                               "Shia", "Tribe Dummy",
                               "Log Swiss Vote", 
                               "By-election",
                               "Inflation",
                               "Law Type - Budget", "Law Type - Treaty",
                               "Law Type - General",
                               "New Law",
                               "WTI x Ideo",
                               "Oil x Ideo"),
          add.lines = list(
            c("Term FEs",  rep("\\checkmark", 4))),
          dep.var.labels = c("Voting with regime"),
          dep.var.caption = "",
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.1, 0.05, 0.01))
sink()


# Table A6 ----------------------------------------------------------------



# supplementary materials - probit analysis
### warning - runs quite slow!
reg1ap<-  (glm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                 ideology_binary + ideoaffil_n_lag +  
                 inflation_imp +
                 age_imp + female + edu_imp + 
                 career +shia +
                 tribe_binary + log(swissvote_imp)+byelection +
                 type_budget + type_treaty + type_general + law_new, dat,
               family = binomial(link = "probit")))

reg1bp<- (glm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                ideology_binary + ideoaffil_n_lag +  
                inflation_imp +
                unique_id +
                type_budget + type_treaty + type_general + law_new, dat,
              family = binomial(link = "probit")))


reg2ap<- (glm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
                ideology_binary + ideoaffil_n_lag +  
                inflation_imp +
                age_imp + female + edu_imp + 
                career +shia +
                tribe_binary +log(swissvote_imp)+byelection +
                type_budget + type_treaty + type_general + law_new, dat,
              family = binomial(link = "probit")))

reg2bp<- (glm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
                ideology_binary + ideoaffil_n_lag +  
                inflation_imp +
                unique_id +
                type_budget + type_treaty + type_general + law_new, dat,
              family = binomial(link = "probit")))

# Table A6
sink("TableA6.tex")
stargazer(reg1ap, reg1bp, reg2ap, reg2bp,
          keep = c("wticonstant*", "oil_gross*", "ideo*", "infl"),
          covariate.labels = c("WTI Price", "Oil Revenues", "Ideology Dummy", "Cabinet Affiliate",
                               "Inflation"
          ),
          add.lines = list(c("Term FEs", rep("\\checkmark", 4)),
                           c("MP FEs", rep(c("", "\\checkmark"), 2)),
                           c("MP controls",rep(c("\\checkmark", ""), 2)),
                           c("Law controls", rep("\\checkmark", 4))),
          dep.var.labels = "Voting with regime",
          omit.stat = c("ser", "adj.rsq", "f"),
          float = F,
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"))
sink()



# Table A7 ----------------------------------------------------------------


abs<- mpdf %>%
  mutate(abs_pro = ifelse(date <= ymd("2007-04-29"), votewithmin_abs, votewithmin_rulechange),
         abs_neg = votewithmin) %>%
  filter(min_exact == 0)

## abstention = pro-regime pre 2007
abspro1 <- (lm(abs_pro ~ wticonstant_lag_std + term + 
                 ideology_binary + ideoaffil_n_lag +  
                 inflation_imp +
                 unique_id +
                 type_budget + type_treaty + type_general + law_new, abs))

abspro1a<-  (lm(abs_pro ~ wticonstant_lag_std + term + 
                  ideology_binary + ideoaffil_n_lag +  
                  age_imp + female + edu_imp + 
                  career +shia +
                  tribe_binary + log(swissvote_imp)+byelection +
                  inflation_imp +
                  type_budget + type_treaty + type_general + law_new, abs))

## abstention = anti-regime pre 2007
absneg1<- (lm(abs_neg ~ wticonstant_lag_std + term + 
                ideology_binary + ideoaffil_n_lag +  
                inflation_imp +
                unique_id +
                type_budget + type_treaty + type_general + law_new, abs))

absneg1a<-  (lm(abs_neg ~ wticonstant_lag_std + term + 
                  ideology_binary + ideoaffil_n_lag +  
                  age_imp + female + edu_imp + 
                  career +shia +
                  tribe_binary + log(swissvote_imp)+byelection +
                  inflation_imp +
                  type_budget + type_treaty + type_general + law_new, abs))

# Table A7
sink("TableA7.tex")
stargazer(abspro1a, abspro1, absneg1a, absneg1, 
          keep = c("wticonstant*",  "ideo*", "infl"),
          covariate.labels = c("WTI Price","Ideology Dummy", "Cabinet Affiliate",
                               "Inflation"
          ),
          add.lines = list(c("Term FEs", rep("\\checkmark", 6)),
                           c("MP FEs", rep(c("", "\\checkmark"), 3)),
                           c("MP controls",rep(c("\\checkmark", ""), 3)),
                           c("Law controls", rep("\\checkmark", 6))),
          dep.var.labels = c("Voting with regime - ", "Voting with regime - "),
          column.labels   = c("Abstention coded as Anti-Regime", "Abstention coded as Pro-Regime"),
          column.separate = c(2, 2),
          omit.stat = c("ser", "adj.rsq", "f"),
          float = F,
          star.cutoffs = c(0.05, 0.01, 0.001))
sink()


# Table A8 ----------------------------------------------------------------

# absences
mpdf_absence<- mins %>% 
  select(lawcodes, term, minfavor) %>% 
  full_join(votedf) %>%
  filter(mp_exact == 1 & min_exact == 0) %>%
  # drop votes where no ministers took a stand and split votes
  filter(!(lawcodes %in% allabs$lawcodes)) %>%
  filter(!(lawcodes %in% splitvotes$lawcodes)) %>%
  #  mutate(postlib = as.numeric(term_year >= 1996))
  mutate(rulechange = as.numeric(date > ymd("2007-04-29"))) %>% 
  # code agreement with ministers
  # vote yes on favor, no on unfavor, 0 otherwise
  mutate(votewithmin = ifelse(minfavor == 1 & vote == "yes", 1, 
                              ifelse(minfavor == 0 & vote == "no", 1, 0)),
         # count abstains as favor
         votewithmin_abs = ifelse(minfavor == 1 & vote == "yes"|vote == "abstain", 1, 
                                  ifelse(minfavor == 0 & vote == "no"|vote == "abstain", 1, 0)),
         abs_no = ifelse(minfavor == 1 & vote == "yes", 1,
                         ifelse((minfavor == 0 & vote == "no"), 1,
                                ifelse(minfavor == 0 & vote == "abstain", 1, 0)))) %>%
  mutate(votewithmin_rulechange = ifelse(rulechange == 0, votewithmin_abs,
                                         ifelse(vote == "abstain", 0, votewithmin))) %>% 
  mutate(abs_pro = ifelse(date <= ymd("2007-04-29"), votewithmin_abs, votewithmin_rulechange),
         abs_neg = votewithmin) %>%
  mutate(vote_abs_pro = ifelse(is.na(abs_pro), 1, abs_pro),
         vote_abs_anti = ifelse(is.na(abs_pro), 0, abs_neg),
         na_vote = as.numeric(is.na(vote)))




## absence + abstention = pro-regime stance
absencepro1 <- (lm(vote_abs_pro ~ wticonstant_lag_std + term + 
                     ideology_binary + ideoaffil_n_lag +  
                     inflation_imp +
                     unique_id +
                     type_budget + type_treaty + type_general + law_new, 
                   mpdf_absence ))


# absence + abstention = anti-regime
absenceneg1 <- (lm(vote_abs_anti ~ wticonstant_lag_std + term + 
                     ideology_binary + ideoaffil_n_lag +  
                     inflation_imp +
                     unique_id +
                     type_budget + type_treaty + type_general + law_new, 
                   mpdf_absence ))


# Table A8
sink("TableA8.tex")
stargazer(absenceneg1, absencepro1, 
          keep = c("wticonstant*",  "ideo*", "infl"),
          covariate.labels = c("WTI Price","Ideology Dummy", "Cabinet Affiliate",
                               "Inflation"
          ),
          add.lines = list(c("Term FEs", rep("\\checkmark", 6)),
                           c("MP FEs", rep(c( "\\checkmark"), 2)),
                           c("Law controls", rep("\\checkmark", 6))),
          dep.var.labels = c("Voting with regime - ", "Voting with regime - "),
          column.labels   = c("Absence coded as Anti-Regime", "Absence coded as Pro-Regime"),
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.05, 0.01, 0.001))
sink()




# Table A9 ----------------------------------------------------------------



reg1_lag3<- (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                  ideology_binary + ideoaffil_n_lag3 +  
                  unique_id +
                  inflation_imp +
                  type_budget + type_treaty + type_general + law_new, dat))

reg1a_lag3<-  (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                    ideology_binary + ideoaffil_n_lag3 +  
                    age_imp + female + edu_imp + 
                    career +shia +
                    tribe_binary + log(swissvote_imp)+byelection +
                    inflation_imp +
                    type_budget + type_treaty + type_general + law_new, dat))

reg2_lag3<- (lm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
                  ideology_binary + ideoaffil_n_lag3 +  
                  unique_id +
                  inflation_imp +
                  type_budget + type_treaty + type_general + law_new, dat))

reg2a_lag3<- (lm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
                   ideology_binary + ideoaffil_n_lag3 +  
                   age_imp + female + edu_imp + 
                   career +shia +
                   tribe_binary +log(swissvote_imp)+byelection +
                   inflation_imp +
                   type_budget + type_treaty + type_general + law_new, dat))

# robustness to lag specifications - 3 months
# Table A9
sink("TableA9.tex")
stargazer(reg1a_lag3, reg1_lag3, reg2a_lag3, reg2_lag3,
          keep = c("wticonstant*", "oil_gross*", "ideology_binary", "ideoaffil*", "infl"),
          covariate.labels = c("WTI Price", "Oil Revenues", 
                               "Ideology Dummy", "Cabinet Affiliate - 3 month lag",
                               "Inflation"),
          add.lines = list(c("Term FEs", rep("\\checkmark", 8)),
                           c("MP FEs", rep(c("", "\\checkmark"), 4)),
                           c("MP controls",rep(c("\\checkmark", ""), 4)),
                           c("Law controls", rep("\\checkmark", 8))),
          dep.var.labels = "Voting with regime",
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.05, 0.01, 0.001))
sink()


# Table A10 ---------------------------------------------------------------


reg1_lag5<- (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                  ideology_binary + ideoaffil_n_lag5 +  
                  unique_id +
                  inflation_imp +
                  type_budget + type_treaty + type_general + law_new, dat))

reg1a_lag5<-  (lm(votewithmin_rulechange ~ wticonstant_lag_std + term + 
                    ideology_binary + ideoaffil_n_lag5 +  
                    age_imp + female + edu_imp + 
                    career +shia +
                    tribe_binary + log(swissvote_imp)+byelection +
                    inflation_imp +
                    type_budget + type_treaty + type_general + law_new, dat))

reg2_lag5<- (lm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
                  ideology_binary + ideoaffil_n_lag5 +  
                  unique_id +
                  inflation_imp +
                  type_budget + type_treaty + type_general + law_new, dat))

reg2a_lag5<- (lm(votewithmin_rulechange ~ oil_gross_lag_std +  term+  
                   ideology_binary + ideoaffil_n_lag5 +  
                   age_imp + female + edu_imp + 
                   career +shia +
                   tribe_binary +log(swissvote_imp)+byelection +
                   inflation_imp +
                   type_budget + type_treaty + type_general + law_new, dat))

# robustness to lag specifications - 5 months
# Table A10
sink("TableA10.tex")
stargazer(reg1a_lag5, reg1_lag5, reg2a_lag5, reg2_lag5,
          keep = c("wticonstant*", "oil_gross*", "ideology_binary", "ideoaffil*", "infl"),
          covariate.labels = c("WTI Price", "Oil Revenues", 
                               "Ideology Dummy", "Cabinet Affiliate - 5 month lag",
                               "Inflation"),
          add.lines = list(c("Term FEs", rep("\\checkmark", 8)),
                           c("MP FEs", rep(c("", "\\checkmark"), 4)),
                           c("MP controls",rep(c("\\checkmark", ""), 4)),
                           c("Law controls", rep("\\checkmark", 8))),
          dep.var.labels = "Voting with regime",
          omit.stat = c("ser", "adj.rsq", "f"),
          star.cutoffs = c(0.05, 0.01, 0.001))
sink()




# Figure A4 ---------------------------------------------------------------


lawdat<- dat %>%
  filter(mp_exact == 1 & min_exact == 0) %>%
  mutate(lawtype = ifelse(type_budget == 1, "Budget",
                          ifelse(type_general == 1, "General",
                                 ifelse(type_treaty == 1, "Treaty/Int'l",
                                        "Final Account")))) %>%
  group_by(lawtype, lawcodes) %>%
  summarise(coop = mean(votewithmin_rulechange))

# Figure A4
fa4<- lawdat %>%
  group_by(lawtype) %>% mutate(mn = mean(coop)) %>%
  ungroup() %>%
  mutate(Type = fct_reorder(lawtype, (mn))) %>% 
  ggplot(aes(x = Type, y = coop)) + geom_boxplot() + coord_flip() +
  theme_bw() + 
  ylab("Prop. MPs voting with ministers (second deliberation level)") +
  xlab("")

ggsave(fa4, filename = "FigureA4.pdf",
      height = 5, width = 7)



# Figure A3 ---------------------------------------------------------------

votedf %>% ##
  distinct(date,lawcodes)

votedf %>% ##
  distinct(date,lawcodes) %>% 
  separate_rows(everything(), sep = ";") %>% filter(lawcodes !="")

fa3 <-
  ggplot(NULL, aes(x=date)) + 
  geom_histogram(
    binwidth=150,
    position = 'identity',
    data = votedf %>% distinct(date,lawcodes) %>% separate_rows(everything(), sep = ";") %>% filter(lawcodes !=""),
    fill="lightgray") +
  geom_histogram(
    binwidth=150,
    position = 'identity',
    data = votedf %>% distinct(date,lawcodes),
    fill="black") +
  ylab("Laws (count)") + xlab(NULL) +
  theme_bw() +
  theme(
    text = element_text(size=18),
    axis.title = element_text(size=14),
    axis.text.x = element_text(size=12),
    axis.text.y = element_text(size=12),
    axis.line = element_line(colour = "black"),
    legend.position="bottom",
    legend.key.width = unit(2,"cm"),
    legend.text = element_text(size=14),
    legend.title=element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor = element_blank()) +
  scale_x_date(
    date_labels = "%Y",
    breaks = function(x) seq.Date(from = min(as.Date("1960-01-01")), to = max(as.Date("2021-01-01")), by = "5 years"),
    minor_breaks = NULL,
    expand=c(0.01,0)) +
  scale_y_continuous(
    breaks=seq(0, 400, 50),
    limits=c(0, 450),
    minor_breaks = NULL,
    expand = c(0, .02)) +
  geom_vline(xintercept = as.Date(c("1963-1-23", "1967-1-25", "1971-1-23", "1975-1-27", "1981-2-23",
                                    "1985-2-26", "1992-10-5", "1996-10-7", "1999-7-3", "2003-7-6",
                                    "2006-6-29", "2008-5-17", "2009-5-16", "2012-2-1", "2012-12-1",
                                    "2013-7-27", "2016-11-29")), linetype="dashed", color=c("gray"))

ggsave(fa3, filename = "FigureA3.pdf",
      height = 5, width = 7)







####

log_close()
